//
//  TeacherListTableViewController.swift
//  DriverSchoolAPP
//
//  Created by Logel on 2017/5/18.
//  Copyright © 2017年 Logel. All rights reserved.
//

import UIKit

class TeacherListTableViewController: UITableViewController,LgCallBack {
    var tList = [TeacherOne]()
    var pageNo = 1
    let header = MJRefreshNormalHeader()
    let footer = MJRefreshAutoNormalFooter()
    override func viewDidLoad() {
        super.viewDidLoad()
        header.setRefreshingTarget(self, refreshingAction:#selector(self.headerRefresh))
        self.tableView.mj_header = header
        footer.setRefreshingTarget(self, refreshingAction:#selector(self.footerRefresh))
        self.tableView.mj_footer = footer
        getData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return tList.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseCell", for: indexPath) as! TeacherListTableViewCell
        cell.to = tList[indexPath.row]
        // Configure the cell...

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //self.performSegue(withIdentifier: "toTeacherAppraise", sender: nil)
    }
    
    func headerRefresh(){
        print("下拉刷新.")
        sleep(1)
        tList.removeAll()
        //重现生成数据
        pageNo = 1
        getData()
        
    }
    
    func footerRefresh(){
        print("提拉加载")
        sleep(1)
        pageNo += 1
        getData()
    }
    
    func getData() {
        asyn_req_post(method: M_TeacherList, cb: self, p: ["placeId":user.placeId as AnyObject,"pageNo":pageNo as AnyObject,"pageSize":10 as AnyObject])
    }
    
    func callBack(method: String, data: AnyObject) {
        let j = JSON(data)
        print(j)
        let head = parseMobileHead(x: j["MobileHead"])
        if head.code == "1" {
            tList += parseTeacherOneList(all:j["MobileBody"].arrayValue)

        }
        tableView.reloadData()
        //结束刷新
//        self.tableView!.mj_header.endRefreshing()
//        self.tableView!.mj_footer.endRefreshing()
        header.endRefreshing()
        footer.endRefreshing()

    }
    
}
